package base.utils

import android.util.SparseArray
import android.view.View
import android.widget.TextView

/**
 * @author njmsir
 * Created by njmsir on 2017/9/18.
 */
class DialogViewHolder(view: View) {
    var convertView = view
        get() = field
        private set
    private var views = SparseArray<View>()

    companion object {
        fun create(view: View): DialogViewHolder {
            return DialogViewHolder(view)
        }
    }

    private fun <T : View> getView(viewId: Int): T {
        var view = views.get(viewId)
        if (view == null) {
            view = convertView.findViewById(viewId)
            views.put(viewId, view)
        }
        return view as T
    }

    fun setText(viewId: Int, textId: Int) {
        var textView: TextView = getView(viewId)
        textView.setText(textId)
    }

    fun setTextColor(viewId: Int, colorId: Int) {
        var textView: TextView = getView(viewId)
        textView.setTextColor(colorId)
    }

    fun setBackgroundResource(viewId: Int, resId: Int) {
        var view: View = getView(viewId)
        view.setBackgroundResource(resId)
    }

    fun setOnClickListener(viewId: Int, clickListener: View.OnClickListener) {
        val view: View = getView(viewId)
        view.setOnClickListener(clickListener)
    }
}